package com.ln.lnzw.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.blankj.utilcode.util.ActivityUtils;
import com.ln.lnzw.R;
import com.ln.lnzw.app.AppConstant;
import com.ln.lnzw.base.BaseActivity;
import com.ln.lnzw.bean.InquiriesBean;
import com.ln.lnzw.bean.PublicityBean;
import com.ln.lnzw.net.HttpMethods;
import com.ln.lnzw.utils.ToastFactory;
import com.ln.lnzw.view.ClearableEditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by wuwei on 2018/5/4.
 * 办件查询
 */

public class InquiriesActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.et_banjianbianhao)
    ClearableEditText etBanjianbianhao;
    @BindView(R.id.et_shenbaozhuti)
    ClearableEditText etShenbaozhuti;
    @BindView(R.id.rl_tijiao)
    RelativeLayout rlTijiao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inquiries);
        ButterKnife.bind(this);
        setView();
    }

    private void initData() {
        HttpMethods.getInstanceCenter().info.getInquiries(spUtils.getString(AppConstant.USER_UID)/*"8dfda5b2-28fa-4cef-a455-2761946602cb"*/,etBanjianbianhao.getText().toString())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<InquiriesBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(InquiriesBean inquiriesBean) {
                        Log.i("123", "onNext: "+inquiriesBean.toString());
                            if ("200".equals(inquiriesBean.getCode())){
                                 Bundle b  = new Bundle();
                                 if (inquiriesBean.getResult()==null){
                                     ToastFactory.getToast(activity,"未找到该申请编号对应的进度").show();
                                 }else{
                                     b.putString("Zhuangtai",inquiriesBean.getResult().getAcceptStatus());
                                     b.putString("Shenqingbianhao",inquiriesBean.getResult().getApplyNo());
                                     b.putString("Shixiangmingcheng",inquiriesBean.getResult().getItemTitle());
                                     b.putString("Shoulibumen",inquiriesBean.getResult().getAcceptDept());
                                     b.putString("Dangqianzhuangtai",inquiriesBean.getResult().getAcceptStatus());
                                     b.putString("Chuangjianriqi",inquiriesBean.getResult().getApplyTime());
                                     ActivityUtils.startActivity(b,InquiriesResultActivity.class);
                                 }
                            }
                            if ("500".equals(inquiriesBean.getCode())){
                                ToastFactory.getToast(activity,inquiriesBean.getMsg()).show();
                                return;
                            }


                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });


    }
    private void setView() {
        Log.i("123", "setView: "+spUtils.getString(AppConstant.USER_UID));
        etBanjianbianhao.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                editTextIsYes();
                editTextIsNo();
            }
        });
//        etShenbaozhuti.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//
//            }
//
//            @Override
//            public void afterTextChanged(Editable editable) {
//                editTextIsYes();
//                editTextIsNo();
//            }
//        });

    }
    private void editTextIsNo() {
        if (TextUtils.isEmpty(etBanjianbianhao.getText())/*||TextUtils.isEmpty(etShenbaozhuti.getText())*/){
            rlTijiao.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
        }
    }

    private void editTextIsYes() {
        if (!TextUtils.isEmpty(etBanjianbianhao.getText())/*&&!TextUtils.isEmpty(etShenbaozhuti.getText())*/){
            rlTijiao.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
        }
    }
    @OnClick({R.id.iv_back, R.id.rl_tijiao})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.rl_tijiao:
                if (TextUtils.isEmpty(etBanjianbianhao.getText())/*||TextUtils.isEmpty(cetMima.getText())*/){
                    return;
                }
                if (!TextUtils.isEmpty(etBanjianbianhao.getText())/*&&!TextUtils.isEmpty(cetMima.getText())*/){
                    initData();
                }
                break;
        }
    }
}
